package com.banciyuan.bcywebview.biz.write.photoselecotor.pselector;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.util.BaseObserver;
import com.banciyuan.bcywebview.base.view.toast.MyToast;
import com.banciyuan.bcywebview.biz.write.photoselecotor.model.PhotoModel;
import com.banciyuan.bcywebview.utils.file.FileTypeUtil;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by squall on 2017/3/16.
 */

public class PhotoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<PhotoModel> plist = new ArrayList<>();
    private List<PhotoModel> selected = new ArrayList<>();
    private Context mContext;
    private boolean single_mode;


    public PhotoAdapter(Context mContext, List<PhotoModel> plist, List<PhotoModel> selected) {
        this(mContext, plist, selected, false);
    }

    public PhotoAdapter(Context mContext, List<PhotoModel> plist, List<PhotoModel> selected, boolean single_mode) {
        this.plist = plist;
        this.selected = selected;
        this.mContext = mContext;
        this.single_mode = single_mode;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new ShootHolder(View.inflate(mContext, R.layout.camera_layout, null));
        } else {
            return new PhotoHolder(View.inflate(mContext, R.layout.layout_square_photoitem, null));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == 0) {
            ShootHolder shootHolder = (ShootHolder) holder;
            shootHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (selected.size() >= 9) {
                        MyToast.show(mContext, mContext.getString(R.string.max_img_limit_reached));
                    } else {
                        BaseObserver.getInstance().notifyWatchers(BaseObserver.TAKE_PHOTO);
                    }
                }
            });
        } else {
            PhotoHolder photoHolder = (PhotoHolder) holder;
            renderPhoto(photoHolder, plist.get(position - 1), position);
        }
    }

    private void renderPhoto(final PhotoHolder photoHolder, final PhotoModel photoModel, final int position) {
        File file = new File(photoModel.getOriginalPath());
        if (StringUtil.notNullEqual(new FileTypeUtil().getFileByFile(file), "gif")) {
            photoHolder.gif.setVisibility(View.VISIBLE);
        } else {
            photoHolder.gif.setVisibility(View.GONE);
        }
        ImageLoader.getInstance().displayImage("file://" + file.getAbsolutePath(), photoHolder.photo, ImageOptions.options_half);
        if (single_mode) {
            photoHolder.check.setVisibility(View.GONE);
        } else {
            photoHolder.check.setVisibility(View.VISIBLE);
        }
        if (selected.contains(photoModel)) {
            photoModel.setChecked(true);
            photoHolder.photo.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
            photoHolder.check.setImageResource(R.drawable.ic_checkbox_pressed);
        } else {
            photoHolder.check.setImageResource(R.drawable.ic_checkbox_normal);
            photoHolder.photo.clearColorFilter();
        }

        photoHolder.check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!photoModel.isChecked()) {
                    if (selected.size() >= 9) {
                        MyToast.show(mContext, mContext.getString(R.string.max_img_limit_reached));
                    } else if (over10m(photoModel.getOriginalPath())) {
                        MyToast.show(mContext, mContext.getString(R.string.over_10m));
                    } else if (overSize(photoModel.getOriginalPath())) {
                        MyToast.show(mContext, mContext.getString(R.string.over_18000));
                    } else {
                        photoModel.setChecked(!photoModel.isChecked());
                        selected.add(photoModel);
                        photoHolder.photo.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
                        photoHolder.check.setImageResource(R.drawable.ic_checkbox_pressed);
                    }
                } else {
                    photoModel.setChecked(!photoModel.isChecked());
                    selected.remove(photoModel);
                    photoHolder.check.setImageResource(R.drawable.ic_checkbox_normal);
                    photoHolder.photo.clearColorFilter();
                }
                BaseObserver.getInstance().notifyWatchers(BaseObserver.CHANGE_NUM);
            }
        });
        photoHolder.photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!single_mode) {
                    BaseObserver.getInstance().notifyWatchers(BaseObserver.PHOTO_PERVIEW, position);
                } else {
                    BaseObserver.getInstance().notifyWatchers(BaseObserver.PHOTO_SELECT, photoModel.getOriginalPath());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return plist.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        return position == 0 ? 0 : 1;
    }

    private class ShootHolder extends RecyclerView.ViewHolder {

        public ShootHolder(View itemView) {
            super(itemView);
        }
    }

    private class PhotoHolder extends RecyclerView.ViewHolder {

        private ImageView photo;
        private ImageView check;
        private TextView gif;

        public PhotoHolder(View itemView) {
            super(itemView);
            photo = (ImageView) itemView.findViewById(R.id.iv_photo_lpsi);
            check = (ImageView) itemView.findViewById(R.id.cb_photo_lpsi);
            gif = (TextView) itemView.findViewById(R.id.gif_logo);
        }
    }

    private boolean over10m(String srcPath) {
        File file = new File(srcPath);
        return file.exists() && file.length() > 10 * 1024 * 1024;
    }

    private boolean overSize(String srcPath) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(srcPath, options);
        int w = options.outWidth;
        int h = options.outHeight;
        if (w >= 18000 || h >= 18000) {
            return true;
        } else {
            return false;
        }
    }
}
